package com.moxiao.yfjh.module.activity.task.queue;

import com.moxiao.yfjh.module.activity.task.domain.DelayTask;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.UUID;

/**
 * @version 1.0
 * @Author zhk
 * @Date 2024/1/30 15:46
 **/
public class RedisDelayQueue<T> {
    private static final Logger log = LoggerFactory.getLogger(RedisDelayQueue.class);
    /**
     * 延迟队列名称
     */
    private String delayQueueName = "delayQueue";

    private RedisTemplate redisTemplate;

    // 传入redis客户端操作
    public RedisDelayQueue(RedisTemplate redisTemplate, String delayQueueName) {
        this.redisTemplate = redisTemplate;
        this.delayQueueName = delayQueueName;
    }

    /**
     * 设置延迟消息
     */
    public void setDelayTasks(T msg, long delayTime) {
        DelayTask<T> delayTask = new DelayTask<>();
        delayTask.setId(UUID.randomUUID().toString());
        delayTask.setMsg(msg);
        redisTemplate.opsForZSet().add(delayQueueName, JSONObject.toJSONString(delayTask), delayTime);
    }
}
